using Cooking.Core.DataInterfaces;
using Cooking.Core.Model;

namespace Cooking.Core.Services
{
    public class IngredientService : IIngredientService
    {
        private readonly IIngredientDao ingredientDao;

        public IngredientService(IIngredientDao dao)
        {
            ingredientDao = dao;
        }

        /// <summary>
        /// Creates a new ingredient.
        /// </summary>
        /// <param name="description">Name of ingredient</param>
        /// <returns>true when the ingredient is added to the database.</returns>
        public bool CreateIngredient(string description)
        {
            var ingredient = ingredientDao.FindByName(description);
            var success = false;

            if (ingredient == null)
            {
                ingredientDao.Create(new Ingredient(description));
                success = true;
            }

            return success;
        }

        /// <summary>
        /// Attempts to load an ingredient by name.  If the ingredient is not found, it is created.
        /// </summary>
        /// <returns>An ingredient matching the description</returns>
        public Ingredient LoadIngredientOrCreateIfMissing(string description)
        {
            Ingredient ing = ingredientDao.FindByName(description);

            if (ing == null)
            {
                ing = new Ingredient(description);
                ingredientDao.Create(ing);
            }

            return ing;
        }
    }
}